﻿using UnityEngine;
using Framework.Utils.SingletonTemplate;
using System;
using System.Collections.Generic;
using Game;
using Framework.UI;
using View;
using IcecreamView;

public class GuideManager : MgrBase<GuideManager>
{
    private List<string> _curGuides;
    private int _curIndex;
    private string _guidePath;
    private Action _onComplete;
    protected override void InitMge()
    {
        _curGuides = new List<string>();
    }
    
    public bool CheckGuide(int level , int part) 
    {
        //var guideTable = RofManager.Instance.RofGuideTable;
        //for (int i = 0; i < guideTable.RowNum; i++)
        //{
        //    var rowData = guideTable.GetDataByRow(i);

        //    if (rowData.Level == level && rowData.Part == part && Account.Instance.PlayerData.CheckGuide(rowData.Path))
        //    {
        //        _guidePath = rowData.Path;
        //        _curGuides.Clear();
        //        _curGuides.AddRange(rowData.Path.Split(','));
        //        return true;
        //    }
        //}
        return false;
    }

    public void ShowGuide(Action callbcak) 
    {
        if (_curGuides.Count > 0)
        {
            _onComplete = callbcak;
            _curIndex = 0;
            Play();
        }
        else 
        {
            callbcak?.Invoke();
        }
    }

    private void Play() 
    {
        Action action = new Action(this.Next);
        UIManager.Instance.OpenView(UIPanel.GuidePanel, new IC_ViewData("msg", _curGuides[_curIndex], action));
    }

    public void Next() 
    {
        _curIndex++;
        if (_curIndex >= _curGuides.Count)
        {
            OnComplete();
        }
        else 
        {
            Play();
        }
    }

    private void OnComplete() 
    {
        Account.Instance.PlayerData.GuideData.Add(_guidePath);
        Account.Instance.Save(Framework.Save.SaveData.GameSystem);
        _guidePath = null;
        _curIndex = 0;
        _curGuides.Clear();
        _onComplete?.Invoke();
        _onComplete = null;
    }
}